﻿using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System.Web.Mvc;
using Huirui.Cavan.Core.Extension;
using System.Collections.Generic;
using Huirui.Cavan.Domain.Seedwork;
using Newtonsoft.Json;

namespace Huirui.Cavan.Presentation.Controller.StockModule
{
    public class StockAllocateController : EntityController<StockAllocate, string, StockAllocateSearch, IStockAllocateViewModel, IStockAllocateAppService>, IStockAllocateController
    {
        public override string ControllerName
        {
            get { return ControllerConst.STOCKALLOCATE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public StockAllocateController()
        {
            this.Model = ModelFactory.CreateModel<IStockAllocateViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "stockallocate_query")]
        public ActionResult AddStock(string id)
        {
            if (id.IsNotEmpty())
            {
                this.Model.Item = this.AppService.GetById(id);
            }

            return View("~/Views/Stock/v1/addStockAllocate.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "stockallocate_edit")]
        public JsonResult AddStock(StockAllocateViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.StockOut.StockInOutDetails = JsonConvert.DeserializeObject<IList<StockInOutDetail>>(model.StockInOutDetails);
            BoolGenericResult<StockAllocate> result = this.AppService.AddOrUpdate(model.Item);  
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
